package com.bupt.vueblog.service.impl;

import com.bupt.vueblog.dao.UserDAO;
import com.bupt.vueblog.domain.dto.UserChangeDTO;
import com.bupt.vueblog.domain.entity.User;
import com.bupt.vueblog.domain.dto.UserDTO;
import com.bupt.vueblog.service.ArticleService;
import com.bupt.vueblog.service.CommentService;
import com.bupt.vueblog.service.RelationService;
import com.bupt.vueblog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 谭继好
 * @Description
 * @date 2024-04-09 13:49
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private ArticleService articleService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private RelationService relationService;

    @Override
    public User getByName(String name) {
        User res = userDAO.getByName(name);
        return res;
    }

    @Override
    // 并不对外提供单独的接口，只在修改密码时调用，确保原始密码正确
    public User getById(Integer id) {
        User res = userDAO.getById(id);
        return res;
    }

    @Override
    public void insert(UserDTO user) {
        userDAO.insert(user);
    }

    @Override
    public void change(UserChangeDTO user) {
        userDAO.update(user);
    }

    @Override
    @Transactional
    public void delete(Integer id) {
        // 删除对应用户
        userDAO.delete(id);
        // 删除用户的所有博客
        articleService.deleteByUser(id);
        // 删除用户所有评论及对应点赞关系，同时将依赖于评论的评论改为一级评论
        commentService.deleteUser(id);
        // 删除用户对文章和评论的点赞（收藏关系也应该删除，这里略）
        relationService.deleteArticleAndComment(id);
    }
}
